package com.sophic_tech.hkb.controller;


import com.sophic_tech.hkb.entity.Result;
import com.sophic_tech.hkb.service.user.GetUserInfoService;
import com.sophic_tech.hkb.service.user.Ipml.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam("userId") String userId, @RequestHeader("Authorization") String authHeader) {
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            return Result.error(401, "无效的 Authorization 头格式，需以 Bearer 开头");
        }
        String token = authHeader.substring(7).trim();
        return userService.getUserInfo(userId, token);
    }
    @PostMapping("/updateUserName")
    public Result updateUserName(@RequestParam("userId") String userId, @RequestHeader("Authorization") String authHeader, @RequestBody String newUserName) {
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            return Result.error(401, "无效的 Authorization 头格式，需以 Bearer 开头");
        }
        String token = authHeader.substring(7).trim();
        return userService.updateUserName(userId, token, newUserName);
    }
}
